package com.shycloud.mido.task.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.SmMemberTeacherApprovalNew;
import com.shycloud.mido.member.api.entity.OrgTeacherDetailed;
import com.shycloud.mido.member.api.entity.OrgTeacherStudent;
import com.shycloud.mido.task.api.entity.SmMemberTask;
import com.shycloud.mido.task.api.entity.SmMemberTaskComplete;
import com.shycloud.mido.task.api.vo.SmMemberTaskVO;
import com.shycloud.mido.task.mapper.SmMemberTaskMapper;
import com.shycloud.mido.task.mapper.SmMemberTaskCompleteMapper;
import com.shycloud.mido.task.mapper.SmMemberTeacherApprovalNewMapper;
import com.shycloud.mido.task.mapper.MemberMapper;
import com.shycloud.mido.task.mapper.OrgTeacherStudentMapper;
import com.shycloud.mido.task.service.SmMemberTaskService;
import com.shycloud.mido.task.service.TeacherService;
import java.util.HashMap;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class SmMemberTaskServiceImpl extends
		ServiceImpl<SmMemberTaskMapper, SmMemberTask> implements SmMemberTaskService {

	/** 任务模块 mapper */
	private SmMemberTaskMapper taskMapper;
	/** 任务完成情况 mapper */
	private SmMemberTaskCompleteMapper taskCompleteMapper;
	/** 教师认证 mapper */
	private SmMemberTeacherApprovalNewMapper approvalNewMapper;
	/** 会员模块 mapper */
	private MemberMapper memberMapper;
	/** 师生关系模块 mapper */
	private OrgTeacherStudentMapper teacherStudentMapper;

	/** 教师模块 service */
	private TeacherService teacherService;


	/**
	 * 获取vip页面任务列表
	 *
	 * @return java.util.List<com.shycloud.mido.member.api.vo.SmMemberTaskVO>
	 * @author nianhua.jiang
	 * @date 2020/8/28 14:07
	 **/
	@Override
	public List<SmMemberTaskVO> getTaskForVipPage(Integer orgId) {

		List<SmMemberTaskVO> taskResult = taskMapper.getTaskForVipPage(1);

		//获取member信息
		Member member = memberMapper.selectById(SecurityUtils.getMember().getId());

		if (member == null) {
			throw new BusinessException("用户信息获取失败");
		}

		for (SmMemberTaskVO smMemberTaskVO : taskResult) {

			switch (smMemberTaskVO.getId()) {
				//任务1- 申请教师资格并通过
				case 1:
					smMemberTaskVO = checkTask1(smMemberTaskVO, member);
					break;
				case 2:
					smMemberTaskVO = checkTask2(smMemberTaskVO, member, orgId);
					break;
				case 3:
					smMemberTaskVO = checkTask3(smMemberTaskVO, member);
					break;
			}

		}
		return taskResult;
	}

	/**
	 * 判断播放器分享页面是否领取过奖励
	 *
	 * @return java.util.Map
	 * @author nianhua.jiang
	 * @date 2021/7/13 9:59
	 */
	@Override
	public Map checkTaskForPlayerPage() {

		Map<String, Object> resMap = new HashMap<>(1);

		String memberId = SecurityUtils.getMember().getId();

		//判断领取情况
		SmMemberTaskComplete taskComplete = taskCompleteMapper
				.selectOne(new QueryWrapper<SmMemberTaskComplete>()
						.eq(SmMemberTaskComplete.COL_MEMBER_ID, memberId)
						.and(x -> x.eq(SmMemberTaskComplete.COL_TASK_ID, 4)));

		if (taskComplete == null) {
			//没领取过
			resMap.put("canReceive", true);
		} else {
			//领取过,无法再次领取
			resMap.put("canReceive", false);
		}
		return resMap;
	}

	/**
	 * 任务1完成情况 - 申请教师资格并通过
	 *
	 * @param smMemberTaskVO
	 * @return SmMemberTaskVO
	 * @author nianhua.jiang
	 * @date 2020/8/28 15:01
	 **/
	private SmMemberTaskVO checkTask1(SmMemberTaskVO smMemberTaskVO, Member member) {

		//判断任务的完成情况
		SmMemberTeacherApprovalNew teacherApprovalNew = approvalNewMapper
				.getTeacherDetailApp(member.getId());

		if (teacherApprovalNew != null && !"1".equals(teacherApprovalNew.getStatus())) {
			smMemberTaskVO.setComplateStatus("2");
			return smMemberTaskVO;
		} else {
			if (!"1".equals(member.getIsTeacher())) {
				smMemberTaskVO.setComplateStatus("2");
				//跳转页面类型
				Map<String, Object> argumentsMap = MapUtil.newHashMap(1);
				argumentsMap.put("type", smMemberTaskVO.getType());
				smMemberTaskVO.setArguments(argumentsMap);
				return smMemberTaskVO;
			}
		}

		//判断奖励领取情况
		SmMemberTaskComplete taskComplete = taskCompleteMapper
				.selectOne(new QueryWrapper<SmMemberTaskComplete>()
						.eq(SmMemberTaskComplete.COL_MEMBER_ID, member.getId())
						.and(x -> x.eq(SmMemberTaskComplete.COL_TASK_ID, smMemberTaskVO.getId())));

		if (taskComplete == null) {
			smMemberTaskVO.setComplateStatus("3");
		} else {
			smMemberTaskVO.setComplateStatus("1");
		}
		return smMemberTaskVO;
	}

	/**
	 * 任务2 完成情况 - 添加学生数量达到 x 人
	 *
	 * @param smMemberTaskVO
	 * @return SmMemberTaskVO
	 * @author nianhua.jiang
	 * @date 2020/8/28 15:31
	 **/
	private SmMemberTaskVO checkTask2(SmMemberTaskVO smMemberTaskVO, Member member, Integer orgId) {

		//目标绑定学生数量
		Integer targetStudentCount = smMemberTaskVO.getExpand2();

		//查询此用户是否为教师
		if ("0".equals(member.getIsTeacher()) || "".equals(member.getIsTeacher())
				|| member.getIsTeacher() == null) {

			//备用跳转页
			smMemberTaskVO.setView(smMemberTaskVO.getExpand3());
			//备用跳转类型
			smMemberTaskVO.setType(smMemberTaskVO.getExpand4());
			//跳转页面类型
			Map<String, Object> argumentsMap = MapUtil.newHashMap(1);
			argumentsMap.put("type", smMemberTaskVO.getExpand4());

			smMemberTaskVO.setArguments(argumentsMap);
			smMemberTaskVO.setComplateStatus("2");
			return smMemberTaskVO;
		}

		OrgTeacherDetailed teacher = teacherService.getTeacher(orgId, member.getId());
		//查询师生关系表,取出数量
		Integer studentCount = teacherStudentMapper
				.selectCount(new QueryWrapper<OrgTeacherStudent>()
						.eq(OrgTeacherStudent.COL_TEACHER_ID, teacher.getId())
						.and(x -> x.eq(OrgTeacherStudent.COL_ORG_ID, orgId)));

		smMemberTaskVO.setExpand1(studentCount);

		//跳转页面类型
		Map<String, Object> argumentsMap = MapUtil.newHashMap(1);
		argumentsMap.put("leftIndex", smMemberTaskVO.getType());
		smMemberTaskVO.setArguments(argumentsMap);

		if (targetStudentCount == studentCount) {
			//检查是否领取
			SmMemberTaskComplete taskComplete = taskCompleteMapper
					.selectOne(new QueryWrapper<SmMemberTaskComplete>()
							.eq(SmMemberTaskComplete.COL_MEMBER_ID, member.getId())
							.and(x -> x
									.eq(SmMemberTaskComplete.COL_TASK_ID, smMemberTaskVO.getId())));
			if (taskComplete == null) {
				smMemberTaskVO.setComplateStatus("3");
			} else {
				smMemberTaskVO.setComplateStatus("1");
			}
		} else {
			smMemberTaskVO.setComplateStatus("2");
		}

		return smMemberTaskVO;
	}

	/**
	 * 任务 3 完成情况 - 新注册用户领取 x 天会员
	 *
	 * @param smMemberTaskVO
	 * @return SmMemberTaskVO
	 * @author nianhua.jiang
	 * @date 2020/8/28 15:31
	 **/
	private SmMemberTaskVO checkTask3(SmMemberTaskVO smMemberTaskVO, Member member) {

		//检查是否领取
		SmMemberTaskComplete taskComplete = taskCompleteMapper
				.selectOne(new QueryWrapper<SmMemberTaskComplete>()
						.eq(SmMemberTaskComplete.COL_MEMBER_ID, member.getId())
						.and(x -> x.eq(SmMemberTaskComplete.COL_TASK_ID, smMemberTaskVO.getId())));
		if (taskComplete == null) {
			smMemberTaskVO.setComplateStatus("3");
		} else {
			smMemberTaskVO.setComplateStatus("1");
		}
		return smMemberTaskVO;

	}

}
